/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.UsertypeModuleDAO;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.UsertypeModule;
import com.cashmate.hibernate.pojo.Usertypes;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Dean
 */
public class EditPrivileges extends ActionSupport {

    private static Logger logger = Logger.getLogger(EditPrivileges.class);
    private String id;//the column in grid which we set key=true, automatically filled up when cell in grid
    //lost its focus and had its value changed.
    private String rewordViewable;
    private String rewordEditable;
    private String rewordCreateable;
    private String rewordDeleteable;
    private UsertypeModuleDAO utypeModule = (UsertypeModuleDAO) ServletActionContext.getServletContext().getAttribute("usermoduleDAO");
    private Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
    private Usertypes ut = (Usertypes) ServletActionContext.getRequest().getSession().getAttribute("usertype");

    @Action(value = "/edit-privileges", results = {
        @Result(name = "error", location = "../error/messages.jsp"),
        @Result(name = "duplicate", location = "../error/formError.jsp")
    })
    @Override
    public String execute() {
        UsertypeModule um;
        try {
            um = utypeModule.get(Integer.parseInt(id));
            if (rewordCreateable != null) {
                um.setRewordCreateable(rewordCreateable);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated creatable privileges of " + um.getUtype() + " for module " + um.getModulename() + " to " + rewordCreateable);
            }
            if (rewordDeleteable != null) {
                um.setRewordDeleteable(rewordDeleteable);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated deleteable privileges of " + um.getUtype() + " for module " + um.getModulename() + " to " + rewordDeleteable);
            }
            if (rewordEditable != null) {
                um.setRewordEditable(rewordEditable);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated editable privileges of " + um.getUtype() + " for module " + um.getModulename() + " to " + rewordEditable);
            }
            if (rewordViewable != null) {
                um.setRewordViewable(rewordViewable);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated viewable privileges of " + um.getUtype() + " for module " + um.getModulename() + " to " + rewordViewable);
            }
            utypeModule.update(um);
        } catch (Exception e) {

            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + e.getLocalizedMessage(), e);
            } else {
                logger.fatal(e.getLocalizedMessage(), e);
            }
            addActionError(e.getLocalizedMessage());
            return ERROR;
        }
        return NONE;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setRewordCreateable(String rewordCreateable) {
        this.rewordCreateable = rewordCreateable;
    }

    public void setRewordDeleteable(String rewordDeleteable) {
        this.rewordDeleteable = rewordDeleteable;
    }

    public void setRewordEditable(String rewordEditable) {
        this.rewordEditable = rewordEditable;
    }

    public void setRewordViewable(String rewordViewable) {
        this.rewordViewable = rewordViewable;
    }
}
